import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;


public class SubFileHandler {
	private String aesKey, hmacKey;
	private AES aes;
    private HMAC hmac;
    private String HMACencryptedSubFile;
	private String encryptedSubFile;
	private String decryptedSubFile;
    
	SubFileHandler(){
		this.aesKey = "I like cookies";
		this.hmacKey = "I don't like cookies";
		
		this.aes = new AES(aesKey);
		this.hmac = new HMAC(hmacKey);
		this.HMACencryptedSubFile = "HMAC_en_sub_file.txt";
		this.decryptedSubFile = "de_sub_file.txt";
		this.encryptedSubFile = "en_sub_file.txt";
	}
	
	public boolean subFileHMACCheck(InputStream in, String fileName){
		try{
			byte[] HMAC1 = extractHMAC(in);
			byte[] HMAC2 = hmac.hash(fileName,in);
			
			in.close();
			if (HMAC1 == null || HMAC2 == null)
				return false;
			
			StringBuffer sb1 = new StringBuffer();
		    for (int i = 0; i < HMAC1.length; i++) {
		    	sb1.append(Integer.toString((HMAC1[i] & 0xff) + 0x100, 16).substring(1));
		    }
		    
		    StringBuffer sb2 = new StringBuffer();
		    for (int i = 0; i < HMAC2.length; i++) {
		    	sb2.append(Integer.toString((HMAC2[i] & 0xff) + 0x100, 16).substring(1));
		    }
		    
		    
			System.out.println("subfile: HERE ARE THE HMACS: " + sb1.toString() + " & " + sb2.toString());
			
			if (sb1.toString().equals(sb2.toString())){
				System.out.println("HMAC matches. HMAC check passed.");
				return true;
			}
			else {
				System.out.println("HMAC do not match. HMAC check failed.");
				return false;
			}
				
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return false;
	}
	
	public byte[] extractHMAC(InputStream in){
				
		//read in the first 64 characters of the file
		byte[] hash = new byte[32];
    	
		try {
			in.read(hash);
			return hash;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}


	public boolean decryptSubFile(InputStream in, String filename){
		//load server file from server and decrypt into another file
		try {
			aes.decrypt(in , new FileOutputStream(filename));
			return true;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}	
	
	public byte[] saveSubFile(String filename, String unlockedName){
		File d_filename = new File("d_" + filename);
		File en_filename = new File("en_" + filename);		
		byte[] HMAC = new byte[32];
		
		try {
			aes.encrypt(new FileInputStream("d_" + filename), new FileOutputStream("en_"+filename));		
			if(d_filename.exists())
				System.out.println("d_" + filename + " exists");
			
			if(d_filename.delete())
				System.out.println("d_" + filename + " deleted");
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			d_filename.delete();
			return null;
		}
		
		//find the HMAC
		System.out.println("generating subfile..." + unlockedName);
		HMAC = hmac.generateHashSubFile("en_"+filename,filename, unlockedName);		
		en_filename.delete();
		
		return HMAC;
	}
}
